﻿

$(function () {
    _vmBase = new viewModel();

    _vmBase.init();
})

function viewModel() {
    var me = this;
    VueViewModelBase.call(me);
    viewModelExtend.call(me);


    me.tableListUrl = _getTableListUrl;
    me.addUrl = _updateModelUrl;
    me.delUrl = _deleteModelUrl;
    me.tabObj = $('#tb_buttons');

    //重写方法
    me.init = function () {


        me.initTableList();

        //添加监听对象
        me.addMonitor();

        //注册icon 单击事件
        me.registerIconClick();


    }

    me.initTableList = function () {
        var options = {
            search: false,
            columns: [
                {
                    checkbox: true
                },
                {
                    field: 'name',
                    title: '名称'

                },
                {
                    field: 'icon',
                    title: '图标',
                    formatter: function (value, row, index) {
                        return '<i class="' + row.icon+'"></i>';
                    }
                },
                {
                    field: 'code',
                    title: '编码',
                },
                {
                    field: 'status',
                    title: '状态',
                    filterControl: "select",
                    filterData: "var:_isEnabledSelect",
                    formatter: function (value, row, index) {
                        var enabledLabel = '<span class="label label-success"><i class="fa fa-unlock"></i>' + _enabledStr + '</span>';
                        var disabledLabel = '<span class="label label-default"><i class="fa fa-lock"></i>' + _disabledStr + '</span>';
                        if (row.status) {
                            return enabledLabel;
                        } else {
                            return disabledLabel;
                        }
                    }
                },
                {
                    field: 'id',
                    title: '操作',
                    formatter: function (value, row, index) {

                        var enabledEle = '<a href="javascript:void(0)" onclick="_vmBase.onEnabledClick(this,' + index + ',\'' + row.id + '\',\'' + _enabledButtonUrl + '\',\'' + _disabledButtonUrl + '\',event)">' + _enabledStr + '</a>';
                        var disabledEle = '<a href="javascript:void(0)" onclick="_vmBase.onDisabledClick(this,' + index + ',\'' + row.id + '\',\'' + _enabledButtonUrl + '\',\'' + _disabledButtonUrl + '\',event)">' + _disabledStr + '</a>';

                        if (row.status) {
                            return disabledEle;
                        } else if (!row.status) {
                            return enabledEle;
                        }

                    }
                }
            ],
            filterControl: true,
            queryParams: function (params) {
                var filter = "";
                if (params.filter != undefined && params.filter != null) {
                    filter = params.filter;
                }

                var temp = {
                    rows: params.limit,                         //页面大小
                    page: (params.offset / params.limit) + 1,   //页码
                    queryKey: vueModel.queryKey,
                    treeId: vueModel.treeId,
                    filter: filter
                };
                return temp;
            }
        }
        me.initList(options);

    }

}

function viewModelExtend() {
    var me = this;

    me.addMonitor = function (monitorObj) {
        var targetObj = vueModel.entityObj;

        if (monitorObj != null && monitorObj != undefined) {
            me.addMonitorObj(targetObj, monitorObj);
        } else {
            var addObj = {
                Id: '',
                Name: '',
                Code: '',
                Icon: '',
                Status: true,
                Description:''
            };
            me.addMonitorObj(targetObj, addObj);
        }
    }


    me.openSelectIconWin = function () {
        
        _layerIndex= layer.open({
            type: 1,
            title: "图标", //不显示标题
            area: ['900px', '600px'], //宽高
            content: $('#selectIconDiv'), //这里content是一个DOM，注意：最好该元素要存放在body最外层，否则可能被其它的相对元素所影响
            success: function (layero, index) {
                
            }
        });
      
        
    }


    me.registerIconClick = function () {
        

        $('.bs-glyphicons-list li').on('click', function () {
            var cuEle = $(this).children();
            var cuEleClassName = cuEle.attr('class');

            //$('#icon').val(cuEleClassName);

            vueModel.$set(vueModel.entityObj, "Icon", cuEleClassName);

            layer.close(_layerIndex);
        });

        
    }


}

